package com.huxuesong.prcc.ums.controller;

import com.huxuesong.prcc.annotation.IgnoreAuth;
import com.huxuesong.prcc.common.ApiBaseAction;
import com.huxuesong.prcc.dto.UmsMemberDTO;
import com.huxuesong.prcc.dto.UmsMemberTransferInfoDTO;
import com.huxuesong.prcc.sys.entity.SysSetting;
import com.huxuesong.prcc.ums.entity.UmsMember;
import com.huxuesong.prcc.ums.service.IUmsMemberService;
import com.huxuesong.prcc.util.CommonResult;
import com.huxuesong.prcc.util.JsonUtils;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 
 * </p>
 *
 * @author hxs
 * @since 2019-08-02
 */
@Slf4j
@RestController
@Api(tags = "UmsMemberController", description = "会员管理系统")
@RequestMapping("/api/member")
public class UmsMemberController extends ApiBaseAction {
        @Autowired
        private IUmsMemberService memberService;

        @Value("${jwt.tokenHeader}")
        private String tokenHeader;
        @Value("${jwt.tokenHead}")
        private String tokenHead;



        @IgnoreAuth
        @ApiOperation(value = "登录以后返回token")
        @GetMapping(value = "/login")
        @ResponseBody
        public Object login(UmsMember umsMember) {
                if (umsMember==null){
                        return new CommonResult().validateFailed("用户名或密码错误");
                }

                return memberService.login(umsMember.getUsername(),umsMember.getPassword());

        }

        @IgnoreAuth
        @ApiOperation("注册")
        @RequestMapping(value = "/reg",method = RequestMethod.POST)
        @ResponseBody
        public Object register(UmsMemberDTO umsMemberDTO) {
                if (umsMemberDTO==null){
                        return new CommonResult().validateFailed("请填写注册信息");
                }


                return memberService.register(umsMemberDTO);
}


        @IgnoreAuth
        @ApiOperation("获取验证码")
        @RequestMapping(value = "/getAuthCode", method = RequestMethod.GET)
        @ResponseBody
        public Object getAuthCode(@Param("phone") String phone ) {
                if (StringUtils.isNullOrEmpty(phone)){
                        return new CommonResult().validateFailed("请传入正确的手机号");
                }
                return memberService.generateAuthCode(phone);
        }

        @IgnoreAuth
        @ApiOperation("修改密码")
        @RequestMapping(value = "/findPassword", method = RequestMethod.POST)
        @ResponseBody
        public Object updatePassword(UmsMemberDTO umsMemberDTO) {
                return memberService.updatePassword(umsMemberDTO);
        }

        @ApiOperation(value = "刷新token")
        @RequestMapping(value = "/token/refresh", method = RequestMethod.GET)
        @ResponseBody
        public Object refreshToken(HttpServletRequest request) {
                String token = request.getHeader(tokenHeader);
                String refreshToken = memberService.refreshToken(token);
                if (refreshToken == null) {
                        return new CommonResult().failed();
                }
                Map<String, String> tokenMap = new HashMap<>();
                tokenMap.put("token", token);
                tokenMap.put("tokenHead", tokenHead);
                return new CommonResult().success(tokenMap);
        }


        @GetMapping("/createCoinAddress")
        @ResponseBody
        public Object createCoinAddress() {
                return memberService.createCoinAddress();
        }


        @ApiOperation("申请转账")
        @RequestMapping(value = "/transfer", method = RequestMethod.POST)
        @ResponseBody
        public Object transfer(UmsMemberTransferInfoDTO transferInfoDTO) {
                if (transferInfoDTO == null){
                        return new CommonResult().failed("参数不能为空");
                }
                log.info("transfer params is :" + JsonUtils.objectToJson(transferInfoDTO));
                return memberService.transfer(transferInfoDTO);
        }


        @ApiOperation("账户详情")
        @RequestMapping(value = "/transferDetail", method = RequestMethod.GET)
        @ResponseBody
        public Object transferDetail(@RequestParam(value = "type", defaultValue = "1") Integer type,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
                return memberService.transferDetail(type,pageNum,pageSize);
        }



        @ApiOperation(value = "登出功能")
        @RequestMapping(value = "/logout", method = RequestMethod.POST)
        @ResponseBody
        public Object logout() {
                return new CommonResult().success(null);
        }


        @ApiOperation(value = "购买摇钱树")
        @RequestMapping(value = "/buytree", method = RequestMethod.GET)
        @ResponseBody
        public Object buyTree() {
                UmsMember member = this.getCurrentMember();
                return memberService.buyTree(member);
        }

        @ApiOperation(value = "摇一摇解锁prcc功能")
        @RequestMapping(value = "/shocktree", method = RequestMethod.GET)
        @ResponseBody
        public Object shockTree() {
                UmsMember member = this.getCurrentMember();
                return memberService.shockTree(member);
        }


        @ApiOperation(value = "获得用户信息")
        @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
        @ResponseBody
        public Object getUserInfo() {
                UmsMember member = this.getCurrentMember();
                return new CommonResult().success(member);
        }


        @ApiOperation(value = "对一些可以取消的转账进行取消")
        @RequestMapping(value = "/canceltransfer", method = RequestMethod.POST)
        @ResponseBody
        public Object getUserInfo(@RequestParam(value = "transferId", defaultValue = "-1") Integer transferId) {

                UmsMember member = this.getCurrentMember();
                return memberService.canceltransfer(member,transferId);

        }

        @ApiOperation(value = "提交提现申请")
        @RequestMapping(value = "/withdrawprcc", method = RequestMethod.POST)
        @ResponseBody
        public Object withdrawprcc(@RequestParam(value = "prcc") BigDecimal prcc,
                                   @RequestParam(value = "recvCoinAddress") String recvCoinAddress) {

                UmsMember member = this.getCurrentMember();
                return memberService.withdrawprcc(member,prcc,recvCoinAddress);

        }

        @ApiOperation(value = "系统配置接口")
        @RequestMapping(value = "/settings", method = RequestMethod.GET)
        @ResponseBody
        public Object getSettings() {

                return memberService.getSysSetting();

        }




}
